﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using eepg_contratos.Models;
using System.Text;
using System.Data;
using System.Collections;
using System.Security;
using System.Security.Cryptography;
using System.IO;

namespace eepg_contratos.Utilities
{
    public class AppUtil
    {
        public String sqlFilter(ContratoModel c)
        {
            String filter = "";

            if (c.contrato.Trim().Length > 0)
            { filter += "and c.CONTRATO like '%" + c.contrato + "%' "; }

            if (c.proceso.Trim().Length > 0)
            { filter += "and c.PROCESO like '%" + c.proceso + "%' "; }

            if (c.fecha_susc_ini.Trim().Length > 0 && c.fecha_susc_fin.Trim().Length == 0)
            { filter += "and c.fecha_suscripcion >= TO_DATE('" + c.fecha_susc_ini + "', 'YYYY-MM-DD') "; }

            if (c.fecha_susc_fin.Trim().Length > 0 && c.fecha_susc_ini.Trim().Length == 0)
            { filter += "and c.fecha_suscripcion <= TO_DATE('" + c.fecha_susc_fin + "', 'YYYY-MM-DD') "; }

            if (c.fecha_susc_fin.Trim().Length > 0 && c.fecha_susc_ini.Trim().Length > 0)
            {
                filter += "and ( c.fecha_suscripcion BETWEEN TO_DATE('" + c.fecha_susc_ini + "', 'YYYY-MM-DD') ";
                filter += "and TO_DATE('" + c.fecha_susc_fin + "', 'YYYY-MM-DD') ) ";
            }

            if (c.fecha_ing_ini.Trim().Length > 0 && c.fecha_ing_fin.Trim().Length == 0)
            { filter += "and c.fecha_ing >= TO_DATE('" + c.fecha_ing_ini + "', 'YYYY-MM-DD') "; }

            if (c.fecha_ing_fin.Trim().Length > 0 && c.fecha_ing_ini.Trim().Length == 0)
            { filter += "and c.fecha_ing <= TO_DATE('" + c.fecha_ing_fin + "', 'YYYY-MM-DD') "; }

            if (c.fecha_ing_fin.Trim().Length > 0 && c.fecha_ing_ini.Trim().Length > 0)
            {
                filter += "and ( c.fecha_ing BETWEEN TO_DATE('" + c.fecha_ing_ini + "', 'YYYY-MM-DD') ";
                filter += "and TO_DATE('" + c.fecha_ing_fin + "', 'YYYY-MM-DD') ) ";
            }

            if (c.tipo.Trim().Length > 0 && c.tipo != "0")
            { filter += "and c.TIPO = '" + c.tipo + "' "; }

            if (c.id_estado.Trim().Length > 0 && c.id_estado != "0")
            { filter += "and c.ID_ESTADO = '" + c.id_estado + "' "; }

            if (c.descripcion.Trim().Length > 0)
            {
                filter += "and ( "; 
                filter += "c.TITULO like '%" + c.descripcion + "%' ";
                filter += "or c.DESCRIPCION like '%" + c.descripcion + "%' ";

                List<string> arr = c.descripcion.Split(' ').ToList();
                foreach (var item in arr)
                {
                    filter += "or c.TITULO like '%" + item + "%' ";
                    filter += "or c.DESCRIPCION like '%" + item + "%' ";
                }
                filter += ") ";
            }

            if (c.id_gerencia.Trim().Length > 0)
            {
                filter += "and c.ID_GERENCIA = '" + c.id_gerencia + "' ";
            }

            return filter;
        }

        public String DataTableToHtml(DataTable dt, String dtName)
        {
            string htmlString = "";

            if (dt == null)
            {
                throw new System.ArgumentNullException("");
            }

            StringBuilder htmlBuilder = new StringBuilder();

            htmlBuilder.Append("<table id='" + dtName + "' class='display' cellpadding='0' cellspacing='0' border='0' width='100%' >");

            htmlBuilder.Append("<thead><tr>");
            foreach (DataColumn col in dt.Columns)
            {
                htmlBuilder.Append("<th style='font: 7pt Arial;'>");
                htmlBuilder.Append(col.ColumnName);
                htmlBuilder.Append("</th>");
            }
            htmlBuilder.Append("</tr></thead><tbody>");
            foreach (DataRow row in dt.Rows)
            {
                htmlBuilder.Append("<tr class='trResultados'>");

                foreach (DataColumn col in dt.Columns)
                {
                    htmlBuilder.Append("<td style='font: 7pt Arial;'>");
                    switch (row[col.ColumnName].GetType().Name)
                    {
                        case "String":
                            htmlBuilder.Append("" + row[col.ColumnName].ToString() + "");
                            break;
                        case "DateTime":
                            DateTime fecha = (DateTime) row[col.ColumnName];
                            htmlBuilder.Append("" + fecha.ToString("yyy-MM-dd") + "");
                            break;
                        case "Double":
                            Double valor = (Double) row[col.ColumnName];
                            htmlBuilder.Append("" + valor.ToString("#,###,###.00") + "");
                            break;
                        default:
                            break;
                    }
                    htmlBuilder.Append("</td>");
                }

                htmlBuilder.Append("</tr>");
            }

            htmlBuilder.Append("</tbody></table>");
            htmlString = htmlBuilder.ToString();

            return htmlString;
        }

        public String ArrayToHtml(ArrayList arr, String tipo)
        {
            string htmlString = "";
            int i = 0;

            if (arr == null)
            {
                throw new System.ArgumentNullException("");
            }

            StringBuilder htmlBuilder = new StringBuilder();

            switch (tipo)
            {
                case "table":
                    htmlBuilder.Append("<table id='listadoResultados' class='display' cellpadding='0' cellspacing='0' border='0' >");
                    htmlBuilder.Append("<tbody>");
                    foreach (ListadoModel item in arr)
                    {
                        htmlBuilder.Append("<tr class='trResultados'>");
                        htmlBuilder.Append("<td style='font: 8pt Arial; width: 20px;'><img src='../Images/item.png'>");
                        htmlBuilder.Append("</td>");
                        htmlBuilder.Append("<td style='font: 8pt Arial;'><div id='" + item.id + "' class='btItem'>");
                        htmlBuilder.Append("" + item.descripcion.ToString() + "");
                        htmlBuilder.Append("</td>");
                        htmlBuilder.Append("</tr>");
                    }

                    htmlBuilder.Append("</tbody></table>");
                    htmlString = htmlBuilder.ToString();
                    break;
                case "option":
                    htmlBuilder.Append("<option value='0'>&nbsp</option>");
                    foreach (ListadoModel item in arr)
                    {
                        htmlBuilder.Append("<option value='");
                        htmlBuilder.Append("" + item.id.ToString() + "");
                        htmlBuilder.Append("'>");
                        htmlBuilder.Append("" + item.descripcion.ToString() + "");
                        htmlBuilder.Append("</option>");
                    }
                    htmlString = htmlBuilder.ToString();
                    break;
                case "only_rows":
                    foreach (ListadoModel item in arr)
                    {
                        i++;
                        htmlBuilder.Append("<tr class='trResultados'>");
                        htmlBuilder.Append("<td style='font: 8pt Arial; width: 20px;'><img src='../Images/item.png'>");
                        htmlBuilder.Append("</td>");
                        htmlBuilder.Append("<td><div id='value1_" + i + "' >");
                        htmlBuilder.Append("" + item.id.ToString() + "");
                        htmlBuilder.Append("</div></td>");
                        htmlBuilder.Append("<td><div id='value2_" + i + "' >");
                        htmlBuilder.Append("" + item.descripcion.ToString() + "");
                        htmlBuilder.Append("</div></td>");

                        htmlBuilder.Append("<td><div id='value3_" + i + "' >");
                        htmlBuilder.Append("<button id='bt_value3_" + i + "' class='btnValue3'>Procesar</button>");
                        htmlBuilder.Append("</div></td>");

                        htmlBuilder.Append("<td><div id='value4_" + i + "' >");
                        htmlBuilder.Append("" + item.numero.ToString() + "");
                        htmlBuilder.Append("</div></td>");

                        htmlBuilder.Append("<td><div id='value5_" + i + "' >");
                        htmlBuilder.Append("");
                        htmlBuilder.Append("</div></td>");

                        htmlBuilder.Append("<td><div id='value6_" + i + "' >");
                        htmlBuilder.Append("" + item.mensaje.ToString() + "");
                        htmlBuilder.Append("</div></td>");

                        htmlBuilder.Append("</tr>");
                    }
                    htmlString = htmlBuilder.ToString();
                    break;
                case "csv":

                    foreach (ListadoModel item in arr)
                    {
                        i++;
                        htmlBuilder.Append("" + item.id.ToString() + ",");
                        htmlBuilder.Append("" + item.descripcion.ToString() + "");
                        htmlBuilder.Append("\n");
                    }
                    htmlString = htmlBuilder.ToString();
                    break;
                default:
                    htmlString = "";
                    break;
            }
            return htmlString;
        }

        public String ContratoArrayToXml(ArrayList arr)
        {
            String htmlString = "";
            if (arr == null)
            {
                throw new System.ArgumentNullException("");
            }

            StringBuilder htmlBuilder = new StringBuilder();
            htmlBuilder.Append("<?xml version='1.0' encoding='UTF-8'?>");
            htmlBuilder.Append("<div id='contrato'>");
            foreach (ContratoModel c in arr)
            {
                htmlBuilder.Append("<div id='codigo'>");
                htmlBuilder.Append("" + c.contrato.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='id'>");
                htmlBuilder.Append("" + c.id.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='titulo'>");
                htmlBuilder.Append("" + c.titulo.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='descripcion'>");
                htmlBuilder.Append("" + c.descripcion.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='tipo'>");
                htmlBuilder.Append("" + c.tipo.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='id_estado'>");
                htmlBuilder.Append("" + c.id_estado.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='proceso'>");
                htmlBuilder.Append("" + c.proceso.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='monto'>");
                htmlBuilder.Append("" + c.monto.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='es_contratista'>");
                htmlBuilder.Append("" + c.es_contratista.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='id_contratista'>");
                htmlBuilder.Append("" + c.id_contratista.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='contratista'>");
                htmlBuilder.Append("" + c.contratista.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='id_gerencia'>");
                htmlBuilder.Append("" + c.id_gerencia.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='gerencia'>");
                htmlBuilder.Append("" + c.gerencia.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='cod_administrador'>");
                htmlBuilder.Append("" + c.cod_administrador.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='administrador'>");
                htmlBuilder.Append("" + c.administrador.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='cod_delegado'>");
                htmlBuilder.Append("" + c.cod_delegado.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='delegado'>");
                htmlBuilder.Append("" + c.delegado.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='fecha_suscripcion'>");
                htmlBuilder.Append("" + c.fecha_suscripcion.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='fecha_inicio'>");
                htmlBuilder.Append("" + c.fecha_inicio.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='fecha_fin'>");
                htmlBuilder.Append("" + c.fecha_fin.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='fecha_culminacion'>");
                htmlBuilder.Append("" + c.fecha_culminacion.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='resolucion'>");
                htmlBuilder.Append("" + c.resolucion.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='fecha_resolucion'>");
                htmlBuilder.Append("" + c.fecha_resolucion.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='observacion'>");
                htmlBuilder.Append("" + c.observacion.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='contratista_representante'>");
                htmlBuilder.Append("" + c.contratista_representante.ToString() + "");
                htmlBuilder.Append("</div>");
            }
            htmlBuilder.Append("</div>");
            htmlString = htmlBuilder.ToString();
            return htmlString;
        }

        public String UsuarioArrayToXml(ArrayList arr)
        {
            String htmlString = "";
            if (arr == null)
            {
                throw new System.ArgumentNullException("");
            }

            StringBuilder htmlBuilder = new StringBuilder();
            htmlBuilder.Append("<?xml version='1.0' encoding='UTF-8'?>");
            htmlBuilder.Append("<div id='usuario'>");
            foreach (UsuarioModel u in arr)
            {
                htmlBuilder.Append("<div id='guid'>");
                htmlBuilder.Append("" + u.guid.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='codigo'>");
                htmlBuilder.Append("" + u.codigo.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='usuario'>");
                htmlBuilder.Append("" + u.usuario.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='password'>");
                htmlBuilder.Append("" + u.password.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='nombre'>");
                htmlBuilder.Append("" + u.nombre.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='observacion'>");
                htmlBuilder.Append("" + u.observacion.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='id_estado'>");
                htmlBuilder.Append("" + u.id_estado.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='guid_rol'>");
                htmlBuilder.Append("" + u.codigo_rol.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='rol'>");
                htmlBuilder.Append("" + u.rol.ToString() + "");
                htmlBuilder.Append("</div>");
            }
            htmlBuilder.Append("</div>");
            htmlString = htmlBuilder.ToString();
            return htmlString;
        }

        public String OpcionArrayToXml(ArrayList arr)
        {
            String htmlString = "";
            if (arr == null)
            {
                throw new System.ArgumentNullException("");
            }

            StringBuilder htmlBuilder = new StringBuilder();
            htmlBuilder.Append("<?xml version='1.0' encoding='UTF-8'?>");
            htmlBuilder.Append("<div id='opcion'>");
            foreach (OpcionModel o in arr)
            {
                htmlBuilder.Append("<div id='guid'>");
                htmlBuilder.Append("" + o.guid.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='descripcion'>");
                htmlBuilder.Append("" + o.descripcion.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='url'>");
                htmlBuilder.Append("" + o.url.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='imagen'>");
                htmlBuilder.Append("" + o.imagen.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='id_estado'>");
                htmlBuilder.Append("" + o.id_estado.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='guid_padre'>");
                htmlBuilder.Append("" + o.guid_padre.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='padre'>");
                htmlBuilder.Append("" + o.padre.ToString() + "");
                htmlBuilder.Append("</div>");
            }
            htmlBuilder.Append("</div>");
            htmlString = htmlBuilder.ToString();
            return htmlString;
        }

        public String PermisoArrayToXml(ArrayList arr)
        {
            String htmlString = "";
            if (arr == null)
            {
                throw new System.ArgumentNullException("");
            }

            StringBuilder htmlBuilder = new StringBuilder();
            htmlBuilder.Append("<?xml version='1.0' encoding='UTF-8'?>");
            htmlBuilder.Append("<div id='permiso'>");
            foreach (RolOpcionModel o in arr)
            {
                htmlBuilder.Append("<div id='guid'>");
                htmlBuilder.Append("" + o.guid.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='guid_opcion'>");
                htmlBuilder.Append("" + o.guid_opcion.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='opcion'>");
                htmlBuilder.Append("" + o.opcion.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='acceder'>");
                htmlBuilder.Append("" + o.acceder.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='borrar'>");
                htmlBuilder.Append("" + o.borrar.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='editar'>");
                htmlBuilder.Append("" + o.editar.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='exportar'>");
                htmlBuilder.Append("" + o.exportar.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='importar'>");
                htmlBuilder.Append("" + o.importar.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='listar'>");
                htmlBuilder.Append("" + o.listar.ToString() + "");
                htmlBuilder.Append("</div>");
                htmlBuilder.Append("<div id='ver'>");
                htmlBuilder.Append("" + o.ver.ToString() + "");
                htmlBuilder.Append("</div>");

            }
            htmlBuilder.Append("</div>");
            htmlString = htmlBuilder.ToString();
            return htmlString;
        }

        public static string FormatURL(string PathWithoutVirtualDirectoryName)
        {
            AppInfo appInformation = AppInfo.GetInstance();
            string DeploymentVirtualDirectory = appInformation.DeploymentVirtualDirectory;

            if (DeploymentVirtualDirectory == "")
            {
                return PathWithoutVirtualDirectoryName;
            }

            StringBuilder SB = new StringBuilder();
            SB.Append(PathWithoutVirtualDirectoryName);
          
            return SB.ToString();
        }

        public static string FullFormatURL(string PathWithoutVirtualDirectoryName)
        {
            AppInfo appInformation = AppInfo.GetInstance();
            string DeploymentVirtualDirectory = appInformation.DeploymentVirtualDirectory;

            if (DeploymentVirtualDirectory == "")
            {
                return PathWithoutVirtualDirectoryName;
            }

            StringBuilder SB = new StringBuilder();
            SB.Append("");
            SB.Append(appInformation.DeploymentVirtualDirectory);
            SB.Append("");
            SB.Append(PathWithoutVirtualDirectoryName);

            return SB.ToString();
        }

        public static String MD5Hash(string text)
        {
            MD5 md5 = new MD5CryptoServiceProvider();

            //compute hash from the bytes of text
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));

            //get hash result after compute it
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }

            return strBuilder.ToString();
        }

        public String ConvertDatatableToXML(DataTable dt)
        {
            MemoryStream str = new MemoryStream();
            dt.WriteXml(str, true);
            str.Seek(0, SeekOrigin.Begin);
            StreamReader sr = new StreamReader(str);
            string xmlstr;
            xmlstr = sr.ReadToEnd();
            return (xmlstr);
        }

    }
}